package com.doyoulike.controller;

import com.doyoulike.common.CommonResult;
import com.doyoulike.entity.GenTable;
import com.doyoulike.enums.GenTypeEnum;
import com.doyoulike.service.table.GenTableService;
import com.doyoulike.vo.in.CreateByIdsVO;
import com.doyoulike.vo.in.DelByGenTableNameVO;
import com.doyoulike.vo.in.ImportTableVO;
import com.doyoulike.vo.in.PageGenTableVO;
import com.doyoulike.vo.out.GeneratedContent;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 独舟
 * @description 表api接口
 * @version 2020-03-21
 */
@Api(value = "生成表接口", tags = "生成表")
@RestController
@RequestMapping(value = "/genTable")
public class GenTableController {

    @Autowired
    GenTableService genTableService;

    @ApiOperation(value = "导入")
    @PostMapping(value = "/import")
    public CommonResult importTable(@RequestBody ImportTableVO importTableVO){
        genTableService.importTable(importTableVO);
        return CommonResult.success();
    }

    @ApiOperation(value = "保存")
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody GenTable genTable){
        genTableService.add(genTable);
        return CommonResult.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping(value = "/update")
    public CommonResult update(@RequestBody GenTable genTable){
        genTableService.update(genTable);
        return CommonResult.success();
    }

    @ApiOperation(value = "详情")
    @PostMapping(value = "/get")
    public CommonResult<GenTable> get(@RequestBody GenTable genTable){
        return CommonResult.success(genTableService.get(genTable));
    }

    @ApiOperation(value = "预览")
    @GetMapping(value = "/preview")
    public CommonResult<List<GeneratedContent>> preview(GenTable genTable){
        List<GeneratedContent> contentList = genTableService.preview(genTable);
        return CommonResult.success(contentList);
    }

    @ApiOperation(value = "批量生成")
    @PostMapping(value = "/createByIds")
    public CommonResult createByIds(@RequestBody CreateByIdsVO ids){
        genTableService.createByIds(ids);
        return CommonResult.success();
    }

    @ApiOperation(value = "批量生成（zip）")
    @PostMapping(value = "/createZipByIds")
    public void createZipByIds(@RequestBody CreateByIdsVO ids, HttpServletResponse response) throws IOException {
        byte[] data = genTableService.createZipByIds(ids);
        genCode(response, data);
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"zui.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/pageList")
    public CommonResult<PageInfo<GenTable>> pageList(@RequestBody PageGenTableVO pageGenTableVO){
        Page<Object> page = PageHelper.startPage(pageGenTableVO.getPageNumber(), pageGenTableVO.getPageSize());
        List<GenTable> list = genTableService.findList(pageGenTableVO);
        PageInfo<GenTable> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(page.getTotal());
        return CommonResult.success(pageInfo);
    }

    @ApiOperation(value = "删除（根据表名）")
    @PostMapping(value = "/delByGenTableName")
    public CommonResult delByGenTableName(@RequestBody DelByGenTableNameVO delByGenTableNameVO){
        genTableService.delByGenTableName(delByGenTableNameVO);
        return CommonResult.success();
    }

    @ApiOperation(value = "获取生成方式")
    @GetMapping(value = "/getGenTypeList")
    public CommonResult getGenTypeList(){
        List<Map<String, String>> genTypeList = new ArrayList<>();
        for (GenTypeEnum value : GenTypeEnum.values()) {
            Map<String, String> map = new HashMap<>();
            map.put("value", value.getKey());
            map.put("label", value.getValue());
            genTypeList.add(map);
        }
        return CommonResult.success(genTypeList);
    }
}